package com.ex.mzwqz.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by Administrator on 2016/6/8.
 */
public class AutoScrollTextView extends TextView implements Runnable {
    private int contentWidth=0;
    private int scrollToX=0;
    private boolean isStop=false;
    private boolean isRun=true;
    private boolean isMeasureContentWidth=false;
    public AutoScrollTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!isMeasureContentWidth) {
            //获取文字长度
            Paint paint=this.getPaint();
            String content=this.getText().toString();
//            contentWidth=(int) paint.measureText(content);
            contentWidth = getMeasuredWidth();
            isMeasureContentWidth=true;
        }
    }
    public void run() {
        if (isRun) {
            if (scrollToX>=contentWidth) {
                //重新开始
                scrollToX=-getMeasuredWidth();
            }
            scrollTo(scrollToX, 0);
            scrollToX=scrollToX+5;
            postDelayed(this, 150);
        }

    }

    // 点击开始,执行线程
    public void startScroll() {
        post(this);
    }

    // 点击暂停
    public void pauseScroll() {
        isRun=false;
    }

    // 点击重新开始
    public void restartScroll() {
        isRun=true;
        scrollToX=0;
        startScroll();
    }

}